<template>

  <div>

    <!--  查询-->
    <el-card>

      <el-form inline label-width="90px" size="mini" class="demo-form-inline">


        <el-form-item label="场所名称">
          <el-input placeholder="请输入场所名称" v-model.trim="queryParam.siteName" clearable size="mini"></el-input>
        </el-form-item>
        <el-form-item label="账号名称">
          <el-input placeholder="请输入账号名称" v-model.trim="queryParam.name" clearable size="mini"></el-input>
        </el-form-item>
        <el-form-item label="账号">
          <el-input placeholder="请输入账号" v-model.trim="queryParam.userName" clearable size="mini"></el-input>
        </el-form-item>
        <el-form-item label="账号类型">
          <el-select v-model="queryParam.type" placeholder="请选择">
            <el-option
              v-for="item in typeArray"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="重要性">
          <el-select v-model="queryParam.userPeculiar" placeholder="请选择" clearable  >
            <el-option key="0" label="特权" :value="0"/>
            <el-option key="1" label="管理员" :value="1"/>
            <el-option key="2" label="一般" :value="2"/>
          </el-select>
        </el-form-item>
        <el-form-item label="绑定类型">
          <el-select v-model="queryParam.bindType" placeholder="请选择" clearable  >
            <el-option key="0" label="环境" :value="0"/>
            <el-option key="1" label="场所" :value="1"/>
            <el-option key="2" label="组件" :value="2"/>
            <el-option key="3" label="工具" :value="3"/>
            <el-option key="4" label="应用" :value="4"/>
          </el-select>
        </el-form-item>



        <el-form-item>
          <el-button type="success" size="mini" @click="getListPage">查询</el-button>
          <el-button type="info" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>

      </el-form>

    </el-card>





    <!--列表-->
    <el-card style="margin-top: 10px">

      <el-table
        :data="tableData"
        max-height="600px"
        height="600px"
        style="width: 100%">


        <el-table-column prop="name" label="名称">
          <template slot-scope="scope">
            <el-link type="primary" :href="getSiteUrl(scope.row.siteId)" target="_blank">
              {{ scope.row.name }}
            </el-link>
          </template>
        </el-table-column>
        <el-table-column prop="siteName" label="现场名称"/>
        <el-table-column prop="userName" label="账号"/>
        <el-table-column prop="typeName" label="账号类型"/>
        <el-table-column prop="userPeculiar" label="重要性">
          <template slot-scope="scope">
            <el-tag size="mini" type="warning" v-if="scope.row.userPeculiar === 1">管理员</el-tag>
            <el-tag size="mini"  v-else-if="scope.row.userPeculiar === 2">一般</el-tag>
            <el-tag size="mini" type="danger" v-else-if="scope.row.userPeculiar === 0">特权</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="bindType" label="绑定类型">
          <template slot-scope="scope">
            <el-tag size="mini" type="warning" v-if="scope.row.bindType === 1">场所</el-tag>
            <el-tag size="mini" type="danger" v-else-if="scope.row.bindType === 0">环境</el-tag>
            <el-tag size="mini"  v-else-if="scope.row.bindType === 2">组件</el-tag>
            <el-tag size="mini"  v-else-if="scope.row.bindType === 3">工具</el-tag>
            <el-tag size="mini"  v-else-if="scope.row.bindType === 4">应用</el-tag>
          </template>
        </el-table-column>


        <el-table-column fixed="right" label="操作" width="60">
          <template slot-scope="scope">
            <el-button @click="openViewBefore(scope.row)" type="text" size="small"><i class="el-icon-view"></i>查看</el-button>
          </template>
        </el-table-column>

      </el-table>


      <!--分页部分-->
      <el-pagination
        style="width:33%; margin-right:10px; margin-top:10px;"
        background
        layout="total, prev, pager, next, jumper"
        @current-change="pageCurrentChange"
        :page-size="pageConfig.size"
        :current-page="pageConfig.current"
        :pager-count="5"
        :total="pageConfig.total">
      </el-pagination>

    </el-card>

    <OpsEnvAccountDetailed ref="OpsEnvAccountDetailedRef"/>

  </div>

</template>

<script>

import http from '@/utils/http'
import OpsEnvAccountDetailed from "../site/OpsEnvAccountDetailed.vue";

export default {
  name: "ComputerList",
  components: {
    OpsEnvAccountDetailed,
  },
  data() {
    return {
      // 查询参数
      queryParam: {
        name: null,
        siteName: null,
        type: null,
        userPeculiar: null,
        bindType: null,
        userName: null,
      },
      // 分页参数
      pageConfig: {
        // 每页记录数
        size: 20,
        // 当前页码
        current: 1,
        // 总记录数
        total: 0,
      },
      // 表格数据
      tableData: [],
      // 账号类型
      typeArray: [],
    }
  },
  methods: {
    /**
     * 分页查询字典
     */
    getDictListPage(){
      http.post("/opsSysDict/getListPage", {
        // 分页参数
        current: 1,
        size: 999,
        sysDictGroupId: "1964968442436055040",
      }).then(response => {
        let res = response.data;
        // 分页数据
        let data = res.data;
        // 处理数据
        this.typeArray = data.records

      }).catch(error => {
        this.$message.error(error.message);
      })
    },
    /**
     * 查看之前触发
     * @param obj 这一行数据
     */
    openViewBefore(obj){
      this.$refs.OpsEnvAccountDetailedRef.init(obj)
    },
    /**
     * 分页查询列表
     */
    getListPage() {
      this.queryParam.current = this.pageConfig.current
      this.queryParam.size = this.pageConfig.size
      http.post("/opsEnvAccount/getListPage", this.queryParam).then(response => {
        let res = response.data;
        // 分页数据
        let data = res.data;
        this.pageConfig.total = data.total;
        this.pageConfig.current = data.current;
        this.pageConfig.size = data.size;
        // 表格数据
        this.tableData = data.records

      }).catch(error => {
        this.$message.error (error.message);
      })
    },
    /**
     * 分页被改变的时候
     * @param value 当前页
     */
    pageCurrentChange(value) {
      this.pageConfig.current = value
      this.getListPage()
    },
    /**
     * 重置查询条件
     */
    resetQuery() {
      this.queryParam ={
        name: null,
        siteName: null,
        type: null,
        userPeculiar: null,
        bindType: null,
        userName: null,
      }
    },
    /**
     * 分页前重置分页参数
     */
    getPageListBefore() {
      this.pageConfig.current = 1
      this.getListPage()
    },
    /**
     * 跳转到场所计算机页面
     * @param siteId 场所id
     * @returns {string}
     */
    getSiteUrl(siteId) {
      // 生成完整URL（需要确保路由配置正确）
      return `${window.location.origin}/#/toSiteLayout?siteId=${siteId}`;
      // 或者如果你的路由是history模式
      // return `${window.location.origin}/toSiteLayout?siteId=${siteId}`;
    },
  },
  // 进入马上执行
  mounted() {
    this.getListPage()
    this.getDictListPage()
    this.$store.commit ('navigationListChange', [
      {name: '全局环境账号', url: '/toAccountList', query: {}},
    ])
  },
}
</script>



<style scoped>

</style>
